@tool
extends Node2D

# 游戏控制器 - 整合所有游戏组件

@export var test = 0

# 预加载脚本
const GameManagerScript = preload("res://scripts/GameManager.gd")
const InputManagerScript = preload("res://scripts/InputManager.gd")
const UIManagerScript = preload("res://scripts/UIManager.gd")
const EffectManagerScript = preload("res://scripts/EffectManager.gd")
const AudioManagerScript = preload("res://scripts/AudioManager.gd")

var game_manager: Node
var input_manager: Node
var ui_manager: Node
var effect_manager: Node
var audio_manager: Node
var card_factory: Node2D
var timer: Timer
var audio_correct: AudioStreamPlayer
var audio_wrong: AudioStreamPlayer
var score_label: Label
var animation_player: Node2D

func _ready():
	print('card_box gd ready')
	initialize_components()
	setup_connections()
	# 直接开始游戏，不显示开始界面
	start_game()
	print(test)

func _process(delta):
	# 只在游戏进行中时更新计时器显示，减少不必要的计算
	if game_manager and game_manager.current_state == GameConstants.GameState.PLAYING and timer and ui_manager:
		var time_left = timer.time_left
		ui_manager.update_timer_display(time_left)

func initialize_components():
	"""初始化所有组件"""
	# 创建游戏管理器
	game_manager = GameManagerScript.new()
	add_child(game_manager)
	
	# 创建输入管理器
	input_manager = InputManagerScript.new()
	add_child(input_manager)
	
	# 创建UI管理器
	ui_manager = UIManagerScript.new()
	add_child(ui_manager)
	ui_manager.set_game_manager(game_manager)
	
	# 创建效果管理器
	effect_manager = EffectManagerScript.new()
	add_child(effect_manager)
	
	# 创建音频管理器
	audio_manager = AudioManagerScript.new()
	add_child(audio_manager)
	
	# 获取现有组件
	card_factory = $CardFactory
	timer = $Timer
	audio_correct = $correctAudio
	audio_wrong = $wrongAudio
	score_label = $Label
	animation_player = $boom1_style
	
	# 初始化卡片
	card_factory.init_card_box()

func setup_connections():
	"""设置信号连接"""
	# 输入信号
	input_manager.input_received.connect(_on_input_received)
	input_manager.pause_requested.connect(_on_pause_requested)
	input_manager.restart_requested.connect(_on_restart_requested)
	
	# UI管理器信号
	ui_manager.start_game_requested.connect(_on_start_game_requested)
	ui_manager.restart_game_requested.connect(_on_restart_game_requested)
	ui_manager.pause_game_requested.connect(_on_pause_game_requested)
	ui_manager.difficulty_changed.connect(_on_difficulty_changed)
	
	# 卡片工厂信号
	card_factory.card_changed.connect(_on_card_changed)
	
	# 定时器信号
	timer.timeout.connect(_on_timer_timeout)

func start_game():
	"""开始游戏"""
	game_manager.start_game()
	start_timer()
	ui_manager.hide_pause_panel()

func start_timer():
	"""启动定时器"""
	timer.wait_time = game_manager.get_timer_duration()
	timer.start()

func _on_input_received(direction: int):
	"""处理输入事件"""
	if game_manager.current_state != GameConstants.GameState.PLAYING:
		return
	
	# 判断输入是否正确
	var is_correct = check_input(direction)
	
	if is_correct:
		handle_correct_input()
	else:
		handle_wrong_input()
	
	# 切换卡片
	change_card()

func check_input(direction: int) -> bool:
	"""检查输入是否正确"""
	var cards_same = card_factory.are_cards_same()
	
	if cards_same and direction == GameConstants.INPUT_RIGHT:
		return true
	elif not cards_same and direction == GameConstants.INPUT_LEFT:
		return true
	else:
		return false

func handle_correct_input():
	"""处理正确输入"""
	# 简单的音效
	audio_correct.play()
	
	# 简单的计分 - 直接更新原来的大记分板
	game_manager.score += 1
	score_label.text = "Score: " + str(game_manager.score)
	
	# 简单的视觉效果
	var card_position = card_factory.global_position
	effect_manager.play_correct_effect(card_position)

func handle_wrong_input():
	"""处理错误输入"""
	audio_wrong.play()
	
	# 简单的视觉效果
	var card_position = card_factory.global_position
	effect_manager.play_wrong_effect(card_position)

func change_card():
	"""切换卡片"""
	card_factory.change_card()
	start_timer()  # 重新启动定时器

func _on_card_changed(previous_index: int, current_index: int):
	"""卡片切换时的处理"""
	animation_player.play_ani2()
	
	# 播放卡片切换音效和效果
	audio_manager.play_card_change_sfx()
	var card_position = card_factory.global_position
	effect_manager.play_card_change_effect(card_position)

func _on_timer_timeout():
	"""定时器超时处理"""
	if game_manager.current_state == GameConstants.GameState.PLAYING:
		# 超时算作错误
		handle_wrong_input()
		change_card()

func _on_score_updated(score: int):
	"""分数更新"""
	score_label.text = "Score: " + str(score)

func _on_combo_updated(combo: int):
	"""连击更新"""
	# 可以在这里添加连击显示效果
	pass

func _on_lives_updated(lives: int):
	"""生命值更新"""
	# 可以在这里添加生命值显示效果
	print("Lives remaining: ", lives)
	
	# 播放生命值丢失效果
	if lives < GameConstants.MAX_LIVES:
		audio_manager.play_lives_lost_sfx()
		effect_manager.play_lives_lost_effect()

func _on_game_ended():
	"""游戏结束"""
	timer.stop()
	input_manager.set_input_enabled(false)
	
	# 播放游戏结束音效和效果
	audio_manager.play_game_over_sfx()
	effect_manager.play_game_over_effect()

func _on_pause_requested():
	"""暂停请求"""
	if game_manager.current_state == GameConstants.GameState.PLAYING:
		game_manager.pause_game()
		timer.paused = true
	elif game_manager.current_state == GameConstants.GameState.PAUSED:
		game_manager.resume_game()
		timer.paused = false

func _on_restart_requested():
	"""重新开始请求"""
	game_manager.restart_game()
	card_factory.reset()
	card_factory.init_card_box()
	input_manager.set_input_enabled(true)
	start_game()

func _on_start_game_requested():
	"""开始游戏请求"""
	start_game()

func _on_restart_game_requested():
	"""重新开始游戏请求"""
	game_manager.restart_game()
	card_factory.reset()
	card_factory.init_card_box()
	input_manager.set_input_enabled(true)
	start_game()

func _on_pause_game_requested():
	"""暂停游戏请求"""
	if game_manager.current_state == GameConstants.GameState.PLAYING:
		game_manager.pause_game()
		timer.paused = true
		ui_manager.show_pause_panel()
	elif game_manager.current_state == GameConstants.GameState.PAUSED:
		game_manager.resume_game()
		timer.paused = false
		ui_manager.hide_pause_panel()

func _on_difficulty_changed(difficulty: GameConstants.Difficulty):
	"""难度改变"""
	game_manager.set_difficulty(difficulty)
